package cn.txun.springboot22.controller;

import cn.txun.springboot22.entity.Emp;
import cn.txun.springboot22.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Controller
public class EmpController {
    @RequestMapping("/add")
    @ResponseBody
    public String add(Emp emp){
        System.out.println("emp = " + emp);
        //获取数据库连接
        try(Connection conn = DBUtils.getConn()) {
            String sql = "insert into emp values(null,?,?,?)";
            //获取传输器
            PreparedStatement ps = conn.prepareStatement(sql);
            //设置参数
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setDouble(3,emp.getSal());
            //执行插入操作，  execute执行  Update更新
            //增删改都用这个方法
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "添加成功,<a href='/'>回到首页</a>";
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public String selectAll(){
        List<Emp> list = new ArrayList<>();
        try (Connection conn = DBUtils.getConn()){
            String sql = "select * from emp";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String job = rs.getString("job");
                double sal = rs.getDouble("sal");
                //将查到的每条数据都封装到一个emp对象中
                //进一步添加到list集合中
                list.add(new Emp(id,name,job,sal));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        String html = "<table border='1' >";
        html+="<tr><th>id</th><th>姓名</th><th>职位</th><th>工资</th><th>操作</th></tr>";
        for(Emp e:list){
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getJob()+"</td>";
            html+="<td>"+e.getSal()+"</td>";
            html+="<td><a href='/delete?id="+e.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(Emp emp){
        System.out.println("emp = " + emp);
        try (Connection conn = DBUtils.getConn()){
            String sql = "update emp set name=?,job=?,sal=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setDouble(3,emp.getSal());
            ps.setInt(4,emp.getId());
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "修改成功，返回<a href='/selectAll'>员工列表</a>";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id){
        System.out.println("id = " + id);
        try (Connection conn = DBUtils.getConn()){
            String sql = "delete from emp where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "删除成功,返回<a href='/selectAll'>员工列表</a>";
    }
}

